package com.mini.wechat.miniwechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.mini.wechat.miniwechat.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;


/**
 * @program: imall-service
 * @description: 爆款榜商品对接
 * @author: 王学伟
 * @create: 2018-11-17 09:30
 **/
@RestController
@RequestMapping("/mini")
public class MiniWechatController {
	private static final Logger logger = LoggerFactory.getLogger(MiniWechatController.class);
	private final String WEATHER_ULR ="http://t.weather.sojson.com/api/weather/city/101030100";
	private final String CAR_URl = "https://api.jisuapi.com/vehiclelimit/query?appkey=281226e9203855a9&city=tianjin&date=";

	Cache<String, Object> manualCache = Caffeine.newBuilder().build();


	@RequestMapping(value = "/wechat", method = { RequestMethod.POST, RequestMethod.GET })
	public String sync(){
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
		Object weaCache = manualCache.getIfPresent("wea_info");
		JSONObject jsonWea = null;
		JSONObject jsonCar = null;
		if(weaCache == null){
			logger.info("----请求接口获取天气----");
			manualCache.put("m_is_use",0);
			jsonWea = json();
//			jsonWea = HttpUtils.getHttpToJSONObject(WEATHER_ULR);
			manualCache.put("wea_info",jsonWea.toString());
		}else{
			jsonWea = JSONObject.parseObject(weaCache.toString());
		}
		Object carCache = manualCache.getIfPresent("car_info");
		if(carCache == null){
			logger.info("----请求接口获取限号----");
			manualCache.put("n_is_use",0);
			jsonCar = jsonCar();
//			jsonCar = HttpUtils.getHttpToJSONObject(CAR_URl+dateStr);
			manualCache.put("car_info",jsonCar.toString());
		}else{
			jsonCar = JSONObject.parseObject(carCache.toString());
		}

		String date = new SimpleDateFormat("yyyy年MM月dd日").format(new Date()).toString();

		String mtime = getMtime();
		String ntime = getNtime();
//		JSONObject jsonWea = json();
//		JSONObject jsonWea = HttpUtils.getHttpToJSONObject(WEATHER_ULR);
//		JSONObject jsonCar = HttpUtils.getHttpToJSONObject(CAR_URl+dateStr);
		StringBuffer sb = new StringBuffer();


		sb.append("[太阳]早上好！[enter]");
		if(jsonWea.getInteger("status") == 200){
			JSONObject jsonData = jsonWea.getJSONObject("data");
			JSONObject todayWea = jsonData.getJSONArray("forecast").getJSONObject(0);
			String week = todayWea.getString("week");
			sb.append("\\UE024"+date+"，"+week+"[enter]");//+星期
			if(jsonCar.getInteger("status") == 0){
				String limitNum = jsonCar.getJSONObject("result").getString("number");
				sb.append("\\UE42E今日限号："+limitNum+"[enter]");
			}
			String wendu = jsonData.getString("wendu")+"℃";
			String shidu = jsonData.getString("shidu");
			String pm25 = jsonData.getString("pm25");
			String quality = jsonData.getString("quality");

			String high = "最"+todayWea.getString("high");
			String low = "最"+todayWea.getString("low");
			String fx = todayWea.getString("fx");
			String fl = todayWea.getString("fl");
			String type = todayWea.getString("type");
			String notice = todayWea.getString("notice");
			sb.append("\\UE335"+high+"，"+low+"[enter]");//温度+湿度
			sb.append("\\UE335当前温度："+wendu+"，湿度："+shidu+"[enter]");//温度+湿度
			sb.append("\\UE335pm2.5："+pm25+"，"+quality+"[enter]");//pm2.5
			sb.append("\\UE335"+fx+fl+"[enter]");//风fx-fl
			sb.append("\\UE327"+type+"-"+notice+"[enter][enter]");//type-notice
		}

		sb.append("\\UE142运动报单站已更新完毕！[enter]");
		sb.append("\\UE233本次更新："+((int) (Math.random() * 100)+100)+"款[enter]");
		sb.append("\\UE233点击以下链接进入选款站[enter]");
		sb.append("\\UE234 https://dwz.cn/diJXDYLi");
		String returnMsg = "";
		String mIsUse = manualCache.getIfPresent("m_is_use").toString();
		String nIsUse = manualCache.getIfPresent("n_is_use").toString();
		//[结束]
		//
		if(mIsUse.equals("0")){
			returnMsg = "{\"rs\":2,\"tip\":\"\",\"ds\":1,\"content\":\""+sb.toString()+"\",\"sendtime\":\""+mtime+"\",\"end\":0}";
			logger.info("早上-收到回调，返回信息："+returnMsg);
			manualCache.put("m_is_use",1);
			return returnMsg;
		}

		if(nIsUse.equals("0")){
//			returnMsg = "{\"rs\":2,\"tip\":\"\",\"ds\":1,\"content\":\""+sb.toString()+"\",\"sendtime\":\""+ntime+"\",\"end\":0}";
			logger.info("晚间-收到回调，返回信息："+returnMsg);
			manualCache.put("n_is_use",1);
			return returnMsg;
		}
		return returnMsg;

	}

	/**
	 * 获取精确到秒的时间戳
	 * @return
	 */
	public String getMtime(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.HOUR_OF_DAY, 17);
		cal.set(Calendar.MINUTE, 10);
		cal.set(Calendar.SECOND, 0);
		System.out.println(cal.getTime());
		String timestamp = String.valueOf(cal.getTime().getTime()/1000);
		return timestamp;
	}

	public String getNtime(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.HOUR_OF_DAY, 22);
		cal.set(Calendar.MINUTE, 00);
		cal.set(Calendar.SECOND, 0);
		System.out.println(cal.getTime());
		String timestamp = String.valueOf(cal.getTime().getTime()/1000);
		return timestamp;
	}
	public String getSecondTimestampTwo(Date date){
		String timestamp = String.valueOf((date.getTime()/1000)+60);
		return timestamp;
	}

	public JSONObject json(){
		String jsonStr = "{\n" +
				"  \"time\": \"2019-06-21 08:40:21\", \n" +
				"  \"cityInfo\": {\n" +
				"    \"city\": \"天津市\", \n" +
				"    \"cityId\": \"101030100\", \n" +
				"    \"parent\": \"天津\", \n" +
				"    \"updateTime\": \"08:16\"\n" +
				"  }, \n" +
				"  \"date\": \"20190621\", \n" +
				"  \"message\": \"Success !\", \n" +
				"  \"status\": 200, \n" +
				"  \"data\": {\n" +
				"    \"shidu\": \"64%\", \n" +
				"    \"pm25\": 83, \n" +
				"    \"pm10\": 42, \n" +
				"    \"quality\": \"轻度污染\", \n" +
				"    \"wendu\": \"26\", \n" +
				"    \"ganmao\": \"儿童、老年人及心脏、呼吸系统疾病患者人群应减少长时间或高强度户外锻炼\", \n" +
				"    \"yesterday\": {\n" +
				"      \"date\": \"20\", \n" +
				"      \"sunrise\": \"04:45\", \n" +
				"      \"high\": \"高温 33.0℃\", \n" +
				"      \"low\": \"低温 24.0℃\", \n" +
				"      \"sunset\": \"19:40\", \n" +
				"      \"aqi\": 128, \n" +
				"      \"ymd\": \"2019-06-20\", \n" +
				"      \"week\": \"星期四\", \n" +
				"      \"fx\": \"东南风\", \n" +
				"      \"fl\": \"<3级\", \n" +
				"      \"type\": \"多云\", \n" +
				"      \"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
				"    }, \n" +
				"    \"forecast\": [\n" +
				"      {\n" +
				"        \"date\": \"21\", \n" +
				"        \"sunrise\": \"04:45\", \n" +
				"        \"high\": \"高温 29.0℃\", \n" +
				"        \"low\": \"低温 23.0℃\", \n" +
				"        \"sunset\": \"19:40\", \n" +
				"        \"aqi\": 128, \n" +
				"        \"ymd\": \"2019-06-21\", \n" +
				"        \"week\": \"星期五\", \n" +
				"        \"fx\": \"东风\", \n" +
				"        \"fl\": \"4-5级\", \n" +
				"        \"type\": \"阴\", \n" +
				"        \"notice\": \"不要被阴云遮挡住好心情\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"22\", \n" +
				"        \"sunrise\": \"04:45\", \n" +
				"        \"high\": \"高温 32.0℃\", \n" +
				"        \"low\": \"低温 23.0℃\", \n" +
				"        \"sunset\": \"19:40\", \n" +
				"        \"aqi\": 95, \n" +
				"        \"ymd\": \"2019-06-22\", \n" +
				"        \"week\": \"星期六\", \n" +
				"        \"fx\": \"北风\", \n" +
				"        \"fl\": \"<3级\", \n" +
				"        \"type\": \"晴\", \n" +
				"        \"notice\": \"愿你拥有比阳光明媚的心情\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"23\", \n" +
				"        \"sunrise\": \"04:45\", \n" +
				"        \"high\": \"高温 36.0℃\", \n" +
				"        \"low\": \"低温 25.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"aqi\": 101, \n" +
				"        \"ymd\": \"2019-06-23\", \n" +
				"        \"week\": \"星期日\", \n" +
				"        \"fx\": \"西南风\", \n" +
				"        \"fl\": \"3-4级\", \n" +
				"        \"type\": \"晴\", \n" +
				"        \"notice\": \"愿你拥有比阳光明媚的心情\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"24\", \n" +
				"        \"sunrise\": \"04:46\", \n" +
				"        \"high\": \"高温 35.0℃\", \n" +
				"        \"low\": \"低温 24.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"aqi\": 100, \n" +
				"        \"ymd\": \"2019-06-24\", \n" +
				"        \"week\": \"星期一\", \n" +
				"        \"fx\": \"西南风\", \n" +
				"        \"fl\": \"3-4级\", \n" +
				"        \"type\": \"多云\", \n" +
				"        \"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"25\", \n" +
				"        \"sunrise\": \"04:46\", \n" +
				"        \"high\": \"高温 34.0℃\", \n" +
				"        \"low\": \"低温 23.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"aqi\": 106, \n" +
				"        \"ymd\": \"2019-06-25\", \n" +
				"        \"week\": \"星期二\", \n" +
				"        \"fx\": \"东南风\", \n" +
				"        \"fl\": \"4-5级\", \n" +
				"        \"type\": \"多云\", \n" +
				"        \"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"26\", \n" +
				"        \"sunrise\": \"04:46\", \n" +
				"        \"high\": \"高温 32.0℃\", \n" +
				"        \"low\": \"低温 22.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"aqi\": 108, \n" +
				"        \"ymd\": \"2019-06-26\", \n" +
				"        \"week\": \"星期三\", \n" +
				"        \"fx\": \"东南风\", \n" +
				"        \"fl\": \"4-5级\", \n" +
				"        \"type\": \"雷阵雨\", \n" +
				"        \"notice\": \"带好雨具，别在树下躲雨\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"27\", \n" +
				"        \"sunrise\": \"04:47\", \n" +
				"        \"high\": \"高温 32.0℃\", \n" +
				"        \"low\": \"低温 23.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-06-27\", \n" +
				"        \"week\": \"星期四\", \n" +
				"        \"fx\": \"东南风\", \n" +
				"        \"fl\": \"<3级\", \n" +
				"        \"type\": \"阴\", \n" +
				"        \"notice\": \"不要被阴云遮挡住好心情\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"28\", \n" +
				"        \"sunrise\": \"04:47\", \n" +
				"        \"high\": \"高温 33.0℃\", \n" +
				"        \"low\": \"低温 23.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-06-28\", \n" +
				"        \"week\": \"星期五\", \n" +
				"        \"fx\": \"东南风\", \n" +
				"        \"fl\": \"<3级\", \n" +
				"        \"type\": \"阴\", \n" +
				"        \"notice\": \"不要被阴云遮挡住好心情\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"29\", \n" +
				"        \"sunrise\": \"04:47\", \n" +
				"        \"high\": \"高温 35.0℃\", \n" +
				"        \"low\": \"低温 22.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-06-29\", \n" +
				"        \"week\": \"星期六\", \n" +
				"        \"fx\": \"东南风\", \n" +
				"        \"fl\": \"<3级\", \n" +
				"        \"type\": \"多云\", \n" +
				"        \"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"30\", \n" +
				"        \"sunrise\": \"04:48\", \n" +
				"        \"high\": \"高温 31.0℃\", \n" +
				"        \"low\": \"低温 21.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-06-30\", \n" +
				"        \"week\": \"星期日\", \n" +
				"        \"fx\": \"东风\", \n" +
				"        \"fl\": \"3-4级\", \n" +
				"        \"type\": \"多云\", \n" +
				"        \"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"01\", \n" +
				"        \"sunrise\": \"04:48\", \n" +
				"        \"high\": \"高温 30.0℃\", \n" +
				"        \"low\": \"低温 21.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-07-01\", \n" +
				"        \"week\": \"星期一\", \n" +
				"        \"fx\": \"北风\", \n" +
				"        \"fl\": \"<3级\", \n" +
				"        \"type\": \"多云\", \n" +
				"        \"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"02\", \n" +
				"        \"sunrise\": \"04:49\", \n" +
				"        \"high\": \"高温 33.0℃\", \n" +
				"        \"low\": \"低温 21.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-07-02\", \n" +
				"        \"week\": \"星期二\", \n" +
				"        \"fx\": \"东南风\", \n" +
				"        \"fl\": \"3-4级\", \n" +
				"        \"type\": \"多云\", \n" +
				"        \"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"03\", \n" +
				"        \"sunrise\": \"04:49\", \n" +
				"        \"high\": \"高温 32.0℃\", \n" +
				"        \"low\": \"低温 21.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-07-03\", \n" +
				"        \"week\": \"星期三\", \n" +
				"        \"fx\": \"东风\", \n" +
				"        \"fl\": \"3-4级\", \n" +
				"        \"type\": \"晴\", \n" +
				"        \"notice\": \"愿你拥有比阳光明媚的心情\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"04\", \n" +
				"        \"sunrise\": \"04:49\", \n" +
				"        \"high\": \"高温 31.0℃\", \n" +
				"        \"low\": \"低温 20.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-07-04\", \n" +
				"        \"week\": \"星期四\", \n" +
				"        \"fx\": \"东南风\", \n" +
				"        \"fl\": \"<3级\", \n" +
				"        \"type\": \"小雨\", \n" +
				"        \"notice\": \"雨虽小，注意保暖别感冒\"\n" +
				"      }, \n" +
				"      {\n" +
				"        \"date\": \"05\", \n" +
				"        \"sunrise\": \"04:50\", \n" +
				"        \"high\": \"高温 32.0℃\", \n" +
				"        \"low\": \"低温 23.0℃\", \n" +
				"        \"sunset\": \"19:41\", \n" +
				"        \"ymd\": \"2019-07-05\", \n" +
				"        \"week\": \"星期五\", \n" +
				"        \"fx\": \"东南风\", \n" +
				"        \"fl\": \"<3级\", \n" +
				"        \"type\": \"小雨\", \n" +
				"        \"notice\": \"雨虽小，注意保暖别感冒\"\n" +
				"      }\n" +
				"    ]\n" +
				"  }\n" +
				"}";
		return JSONObject.parseObject(jsonStr);
	}

	public JSONObject jsonCar(){
		String jCar = "{\n" +
				"    \"status\": 0,\n" +
				"    \"msg\": \"ok\",\n" +
				"    \"result\": {\n" +
				"        \"city\": \"hangzhou\",\n" +
				"        \"cityname\": \"杭州\",\n" +
				"        \"date\": \"2015-12-03\",\n" +
				"        \"week\": \"星期四\",\n" +
				"        \"time\": [\n" +
				"            \"07:00-09:00\",\n" +
				"            \"16:30-18:30\"\n" +
				"        ],\n" +
				"        \"area\": \"1、本市号牌：留祥路—石祥路—石桥路—秋涛路—复兴路—老复兴路—虎跑路—满觉陇路—五老峰隧道—吉庆山隧道—梅灵北路—九里松隧道—灵溪南路—灵溪隧道—西溪路—紫金港路—文一西路—古墩路构成的围合区域内所有道路以及高架(含匝道以及附属桥梁、隧道)。其中留祥路、石祥路、石桥路、秋涛路、复兴路、老复兴路、紫金港路、文一西路和古墩路不含。<br>2、外地号牌：上述“错峰限行”区域以及绕城高速合围区域内的其他高架道路(含匝道以及附属桥梁、隧道)。\",\n" +
				"        \"summary\": \"本市号牌尾号限行，外地号牌全部限行。法定上班的周六周日不限行。\",\n" +
				"        \"numberrule\": \"最后一位数字\",\n" +
				"        \"number\": \"4和6\"\n" +
				"    }\n" +
				"}";

		return JSONObject.parseObject(jCar);
	}



}
